/*
package com.meeting.y.controller;

import com.itextpdf.text.pdf.PdfReader;
import com.sinitek.convert_pdf.service.IWordToPdfService;
import com.sinitek.convert_pdf.utils.ExceptionUtils;
import com.sinitek.convert_pdf.utils.MapUtils;
import com.sinitek.sirm.common.utils.StringUtil;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping( "/wordToPdfService" )
@SuppressWarnings("unchecked")
public class ConvertPDFController
{
    private final static Logger LOGGER = LoggerFactory.getLogger( ConvertPDFController.class );

    // 利用pdf下载接口和导出的excel 下载pdf附件
	@RequestMapping( "/pdfPagesize.action" )
    public Map pdfPagesize( HttpServletRequest request ){

    	// 10.2.144.182:8080 替换为对应项目环境地址
        String url = "http://10.2.144.182:8080/rschreport/innerreport/downloadattachment.action?id=";
        String sessionid = "&_ssid_=789608072";
        // 保存下载的pdf文件路径
        String _pdffilepath = "D:\\work\\pdf\\";

        // sql导出的excel路径
        String _filepath = "C:\\work\\git\\";
        String _filename = "pdfexport.xlsx";
        File file = new File(_filepath+_filename);
        try {
            FileInputStream _in = new FileInputStream(file);
            Workbook workbook = parse(_in, "xlsx");
            Sheet sheet = workbook.getSheetAt(0);
            _in.close();

            Integer userid = 1;
            int rowNum = sheet.getPhysicalNumberOfRows();
            for (int rowIndex = 1; rowIndex < rowNum; rowIndex++) {
                try {
                    boolean flag = true;
                    //信息验证
                    StringBuffer message = new StringBuffer();
                    Row row = sheet.getRow(rowIndex);
                    Map entry = new HashMap();
                    int cellNum = row.getPhysicalNumberOfCells();
                    if (cellNum > 0) {
                        Cell cell1 = row.getCell(1);//att id
                        String attid  = getCellValue(cell1);
                        String requesturl = url+attid+sessionid;
                        System.out.println(requesturl);
                        saveFile(requesturl,_pdffilepath+attid+".pdf");

                        PdfReader reader = new PdfReader(_pdffilepath+attid+".pdf");
                        int pagesize = reader.getNumberOfPages();
                        reader.close();

                        Cell cell2 = row.getCell(2);//att pagecount
                        if(cell2 ==null){
                            cell2 = row.createCell(2);
                        }
                        cell2.setCellValue(pagesize);


                    }
                    if(rowIndex%100 == 0){
                        Thread.sleep(1000);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    _in.close();
                }

            }

            FileOutputStream excelFileOutPutStream = new FileOutputStream(_filepath+_filename);
			// 将最新的 Excel 文件写入到文件输出流中，更新文件信息！
            workbook.write(excelFileOutPutStream);
            // 执行 flush 操作， 将缓存区内的信息更新到文件上
            excelFileOutPutStream.flush();
			// 使用后，及时关闭这个输出流对象， 好习惯，再强调一遍！
            excelFileOutPutStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }

        Map resultMap = new HashMap();
        resultMap.put( "status", "OK" );
        return resultMap;

    }


    // 获取下载pdf附件利用PdfReader类 获取页数并保存值导出的excel中
    @RequestMapping( "/pdfCount.action" )
    public Map pdfCount( HttpServletRequest request ){
		// sql导出的excel路径
        String _filepath = "C:\\work\\git\\";
        String _filename = "pdfexport.xlsx";
        // 生成pdf附件的文件路径
        String _pdffilepath = "D:\\work\\pdf\\";
        File file = new File(_filepath+_filename);
        try {
            FileInputStream _in = new FileInputStream(file);
            Workbook workbook = parse(_in, "xlsx");
            Sheet sheet = workbook.getSheetAt(0);
            _in.close();

            Integer userid = 1;
            //sheet.getPhysicalNumberOfRows();
            int rowNum = sheet.getPhysicalNumberOfRows();
            for (int rowIndex = 1; rowIndex < rowNum; rowIndex++) {
                try {
                    boolean flag = true;
                    //信息验证
                    StringBuffer message = new StringBuffer();
                    Row row = sheet.getRow(rowIndex);
                    Map entry = new HashMap();
                    int cellNum = row.getPhysicalNumberOfCells();
                    if (cellNum > 0) {
                        //att id
                        Cell cell1 = row.getCell(1);
                        String attid  = getCellValue(cell1);
                        Integer pagesize = 0;
                        if(StringUtil.isNotEmpty(attid)){
                            LOGGER.info("_pdffilepath------------: "+_pdffilepath);
                            LOGGER.info("attid-----------: "+attid);
                            PdfReader reader = new PdfReader(_pdffilepath+attid+".pdf");
                            pagesize = reader.getNumberOfPages();
                            if(pagesize == null) {
                                pagesize = 0;
                            }
                            reader.close();
                        }
                        // att pagecount
                        Cell cell2 = row.getCell(2);
                        if(cell2 ==null){
                            cell2 = row.createCell(2);
                        }
                        cell2.setCellValue(pagesize);
                    }
                    if(rowIndex%100 == 0){
                        Thread.sleep(1000);
                    }
                }catch (Exception e){
                    LOGGER.info("e异常-----------: "+e);
                }finally {
                    _in.close();
                }

            }

            FileOutputStream excelFileOutPutStream = new FileOutputStream(_filepath+_filename);
            // 将最新的 Excel 文件写入到文件输出流中，更新文件信息！
            workbook.write(excelFileOutPutStream);
            // 执行 flush 操作， 将缓存区内的信息更新到文件上
            excelFileOutPutStream.flush();
            // 使用后，及时关闭这个输出流对象， 好习惯，再强调一遍！
            excelFileOutPutStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }






        Map resultMap = new HashMap();
        resultMap.put( "status", "OK" );
        return resultMap;
    }

    public void saveFile(String _url,String filename){

        URL url=null;
        InputStream is=null;
        try {
            //以必应首页图片为例
            url = new URL
                    (_url);
            URLConnection urlConnection = url.openConnection();

            // 先登录环境 获取cookie和用户名 替换JSESSIONID，USESSIONID，
            // cluster_username，lastaccessedtime，sso_logontime，usersessionchecktime
            String cookie ="JSESSIONID=EF7C3E6F6EAEB16DCDA86384976A6B48;" +
                    "USESSIONID=bc771efd9ea343cfb825d9134de53850;"
                    +"cluster_username=gejunwen;"
                    +"lastaccessedtime=20191022193101;"
                    +"usersessionchecktime=8DED767BD2FD8DC335BA8E11AA288A49;"
                    +"sso_logontime=20191022193101;";
            urlConnection.setRequestProperty("Cookie", cookie);

            is=urlConnection.getInputStream();//得到输入流
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        //建立缓冲输入流，包装得到的普通输入流
        try(
                BufferedInputStream bif=new BufferedInputStream(is);
			*/
/*
			建立缓冲输出流，包装文件输出流，写入数据到磁盘文件，起名为“1.jpg”,路径为项目路径
			文件保存格式为jpg(或者jpeg)
			*//*

                BufferedOutputStream bof=
                        new BufferedOutputStream(
                                new FileOutputStream(
                                        new File(filename)
                                )
                        )
        )

        {//读写流程
            int len;
            byte[] buffer=new byte[1024*10];
            while (-1!=(len=bif.read(buffer))) {
                bof.write(buffer, 0, len);
            }
            bof.flush();
            bof.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getCellValue(Cell cell){
        NumberFormat formater = NumberFormat.getInstance();
        formater.setGroupingUsed(false);
        String cellValue="";
        if(cell==null){
            return cellValue;
        }
        switch(cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:
                java.math.BigDecimal big = new java.math.BigDecimal(cell.getNumericCellValue());
                cellValue = StringUtil.safeToString(big.toString(), "");
                break;
            case Cell.CELL_TYPE_FORMULA:
                try {
                    cellValue = cell.getStringCellValue();
                } catch (IllegalStateException e) {
                    java.math.BigDecimal big1 = new java.math.BigDecimal(cell.getNumericCellValue());
                    cellValue = StringUtil.safeToString(big1.toString(), "");
                }
                break;
            case HSSFCell.CELL_TYPE_STRING:
                cellValue = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                cellValue = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                cellValue = Boolean.valueOf(cell.getBooleanCellValue()).toString();
                break;
            case HSSFCell.CELL_TYPE_ERROR:
                cellValue = String.valueOf(cell.getErrorCellValue());
                break;
            default:cellValue="";
        }
        return cellValue.trim();
    }

    public static Workbook parse(FileInputStream buffer, String suffix) {
        Workbook text = null;
        switch (suffix) {
            case "xls":
                text = getTextFromExcel(buffer);
                break;
            case "xlsx":
                text = getTextFromExcel2007(buffer);
                break;
        }
        return text;
    }

    // 读取Excel97-2003的全部内容 xls
    private static Workbook getTextFromExcel(FileInputStream file) {
        Workbook wb = null;
        String text = "";
        try {
            wb = new HSSFWorkbook(new POIFSFileSystem(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    // 读取Excel2007+的全部内容 xlsx
    private static Workbook getTextFromExcel2007(FileInputStream file) {
        Workbook workBook = null;
        String text = "";
        try {
            workBook = new XSSFWorkbook(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workBook;
    }
}
*/
